<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="/resources/template/template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ez="http://java.sun.com/jsf/composite/ezcomp"
                xmlns:bi="http://java.sun.com/jsf/composite/bill">



    <ui:define name="content">

        <h:form id="form">
            <p:focus id="focusIx" for="acIx" />
            <h:panelGroup styleClass="alignTop" rendered="#{!labBillCollectingController.printPreview}"  >
                <p:panel style="min-height: 600px; position:relative;"  >
                    <f:facet name="header" >
                        <p:panelGrid columns="3">
                            <p:autoComplete id="acColl" forceSelection="true" value="#{labBillCollectingController.collectingCentre}" completeMethod="#{collectingCentreController.completeCollecting}" var="ix" itemLabel="#{ix.name}" itemValue="#{ix}" size="30"  style="width: 400px;">
                                <p:column>
                                    #{ix.name}
                                </p:column>
                                <p:column>
                                    #{ix.institutionCode}
                                </p:column>
                            </p:autoComplete>
                            <p:watermark for="acColl" value="Collecting Centre" />
                            <p:commandButton ajax="false" action="#{labBillCollectingController.settleBill()}" value="Settle" onclick="onSubmitButton();" />
                            <p:commandButton ajax="false" action="/lab/bill_collecting_centre" actionListener="#{labBillCollectingController.prepareNewBill()}" value="New Bill" onclick="onSubmitButton();" />

                            


                        </p:panelGrid>
                    </f:facet>
                    <h:panelGroup rendered="#{!labBillCollectingController.printPreview}" >

                        <h:panelGrid columns="2" >
                            <h:panelGrid columns="1" >
                                <h:panelGroup id="panelPatient" >
                                    <p:tabView id="tvPt" style="min-height: 250px;"  >
                                        <p:ajax event="tabChange"  process="@this" listener="#{labBillCollectingController.onTabChange}" />

                                        <p:tab id="tabNewPt" title="New Patient"  >
                                           <ez:patientDetail controller="#{labBillCollectingController}"/>  
                                        </p:tab>
                                        <p:tab id="tabSearchPt" title="Search Patient">

                                            <h:panelGrid columns="1" >
                                                <p:autoComplete widgetVar="aPt" id="acPt" forceSelection="true" 
                                                                value="#{labBillCollectingController.searchedPatient}" 
                                                                completeMethod="#{patientController.completePatient}" 
                                                                var="apt" itemLabel="#{apt.person.name}" 
                                                                itemValue="#{apt}" size="30"  style="width: 400px;">
                                                    <p:ajax event="itemSelect" process="acPt" update="panSearch"/>
                                                </p:autoComplete>   

                                                <h:panelGrid id="panSearch" columns="2" >
                                                    <h:outputLabel  rendered="#{labBillCollectingController.searchedPatient!=null}" value="Name" />
                                                    <h:outputLabel rendered="#{labBillCollectingController.searchedPatient!=null}" value="#{labBillCollectingController.searchedPatient.person.name}" ></h:outputLabel>
                                                    <h:outputLabel  rendered="#{labBillCollectingController.searchedPatient!=null}" value="Sex" />
                                                    <h:outputLabel rendered="#{labBillCollectingController.searchedPatient!=null}" value="#{labBillCollectingController.searchedPatient.person.sex}" ></h:outputLabel>
                                                    <h:outputLabel  rendered="#{labBillCollectingController.searchedPatient!=null}" value="DOB"  />

                                                    <h:outputLabel rendered="#{labBillCollectingController.searchedPatient!=null}" value="#{labBillCollectingController.searchedPatient.person.dob}"   >
                                                        <f:convertDateTime pattern="dd/MM/yy" />
                                                    </h:outputLabel>

                                                    <h:outputLabel  rendered="#{labBillCollectingController.searchedPatient!=null}" value="Adress" />
                                                    <h:outputLabel rendered="#{labBillCollectingController.searchedPatient!=null}" value="#{labBillCollectingController.searchedPatient.person.address}" ></h:outputLabel>
                                                </h:panelGrid>
                                            </h:panelGrid>

                                        </p:tab>

                                    </p:tabView>

                                </h:panelGroup>

                                <h:panelGroup id="panelIx" >


                                    <p:panel id='pIxAdd' header="Investigation" style="min-height: 350px;"  >
                                        <p:focus  context="acIx"/>
                                        <p:autoComplete widgetVar="aIx" id="acIx" forceSelection="true" value="#{labBillCollectingController.currentBillItem.item}" completeMethod="#{investigationController.completeInvestWithout}" var="ix" itemLabel="#{ix.name}" itemValue="#{ix}" size="30"  style="width: 400px;">
                                            <p:column>
                                                #{ix.name}
                                            </p:column>
                                            <p:column>
                                                #{ix.total}
                                            </p:column>

                                        </p:autoComplete>
                                        <h:commandButton id="btnAddIx" value="Add" action="#{labBillCollectingController.addToBill()}">
                                            <f:ajax execute="acIx btnAddIx" render="focusIx tblBillItem  pBillDetails pIxAdd" />
                                        </h:commandButton>
                                        <p:defaultCommand target="btnAddIx" />
                                    </p:panel>

                                </h:panelGroup>

                            </h:panelGrid>


                            <h:panelGrid columns="1" >

                                <p:dialog id="panDoc" header="Add New Doctor" widgetVar="dlg" resizable="false">  
                                    <h:panelGrid columns="2" style="margin-bottom:10px">  
                                        <h:outputText id="lblNameD" value="Name" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtNameD"  value="#{doctorController.current.person.name}"  ></h:inputText>
                                        <h:outputText id="lblPhoneD" value="Phone" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtPhoneD" value="#{doctorController.current.person.phone}"  ></h:inputText>
                                        <h:outputText id="lblFaxD" value="Fax" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtFaxD" value="#{doctorController.current.person.fax}"  ></h:inputText>
                                        <h:outputText id="lblMobileD" value="Mobile" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtMobileD" value="#{doctorController.current.person.mobile}"  ></h:inputText>
                                        <h:outputText id="lblAddressD" value="Address" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtAddressD" value="#{doctorController.current.person.address}"  ></h:inputText>
                                        <h:outputText value="Speciality" ></h:outputText>                                    
                                        <h:selectOneListbox id="cmbSpecialityD" size="1" value="#{doctorController.current.speciality}" >
                                            <f:selectItem itemLabel="Please select a speciality"/>
                                            <f:selectItems value="#{specialityController.items}" var="cat" itemLabel="#{cat.name}" itemValue="#{cat}" />
                                        </h:selectOneListbox>
                                        <h:outputText id="lblRegD" value="Registration" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtRegD" value="#{doctorController.current.registration}"  ></h:inputText>                 
                                        <h:outputText id="lblQuaD" value="Qualification" ></h:outputText>
                                        <h:inputText autocomplete="off" id="txtQuaD" value="#{doctorController.current.qualification}"  ></h:inputText>   

                                        <p:commandButton id="btnDocSave" value="Add New" process="btnDocSave panDoc" update="panDoc #{p:component('cmbDoc')}" actionListener="#{doctorController.saveSelected()}" styleClass="buttons" oncomplete="dlg.hide();" onclick="onSubmitButton();" >
                                        </p:commandButton>

                                    </h:panelGrid>  

                                </p:dialog>  


                                <h:panelGroup id="panelBillReferrals" >

                                    <p:tabView >
                                        <p:tab title="Refering Doctor">
                                            <p:autoComplete forceSelection="true" id="cmbDoc" value="#{labBillCollectingController.referredBy}" completeMethod="#{doctorController.completeDoctor}" var="ix" itemLabel="#{ix.person.name}" itemValue="#{ix}" size="30"  style="width: 400px;">
                                            </p:autoComplete>
                                            <p:commandButton id="btnAddNewDoc" type="button" value="Add New Doctor" onclick="dlg.show();" actionListener="#{doctorController.prepareAdd}" process="btnAddNewDoc" update="#{p:component('panDoc')}" />  
                                        </p:tab>
                                                                      
                                        <p:tab title="Staff">
                                            <p:autoComplete forceSelection="true" converter="stfcon" value="#{labBillCollectingController.staff}" completeMethod="#{staffController.completeStaff}" var="ix2" itemLabel="#{ix2.person.name}" itemValue="#{ix2}" size="30"  style="width: 400px;">
                                            </p:autoComplete>
                                        </p:tab>

                                    </p:tabView>

                                </h:panelGroup>

                                <h:panelGroup id="panelBillItems" >

                                    <p:tabView id="tblBillItem" >
                                        <p:tab id="tabBillItems" title="Bill Items" >
                                            <p:dataTable id="tblRequests" rowIndexVar="rowIndex" value="#{labBillCollectingController.lstBillEntries}" var="bi" >

                                                <p:column>
                                                    <f:facet name="header">No</f:facet>
                                                        #{rowIndex+1}
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Item</f:facet>
                                                        #{bi.billItem.item.name}
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Fee</f:facet>
                                                        #{bi.billItem.netValue}
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Department</f:facet>
                                                        #{bi.billItem.item.department.name}
                                                </p:column>

                                                <p:column>
                                                    <h:commandButton id="btnRemove" value="X" action="#{labBillCollectingController.removeBillItem}" >
                                                        <f:setPropertyActionListener  value="#{rowIndex}" target="#{labBillCollectingController.index}" />
                                                        <p:ajax  process="btnRemove" update="@form" />
                                                    </h:commandButton>
                                                </p:column>
                                            </p:dataTable>
                                        </p:tab>
                                        <p:tab id="tabBillIx" title="Tests" >
                                            <p:dataTable rowIndexVar="rowIndex" value="#{labBillCollectingController.lstBillComponents}" var="bix" >
                                                <p:column>
                                                    <f:facet name="header">No</f:facet>
                                                        #{rowIndex+1}
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Name</f:facet>
                                                        #{bix.item.name}
                                                </p:column>

                                            </p:dataTable>
                                        </p:tab>
                                        <p:tab id="tabBillFee" title="Fees" >
                                            <p:dataTable rowIndexVar="rowIndex" value="#{labBillCollectingController.lstBillFees}" var="bif" >
                                                <p:column>
                                                    <f:facet name="header">No</f:facet>
                                                        #{rowIndex+1}
                                                </p:column>
                                                <p:column>
                                                    <f:facet name="header">Fee</f:facet>
                                                    <h:inputText autocomplete="off" id="txtFeeVal" value="#{bif.feeValue}" disabled="#{!bif.billItem.item.userChangable}" >
                                                        <f:ajax event="keyup" execute="txtFeeVal" render=" :#{p:component('tblRequests')}  :#{p:component('pBillDetails')}" listener="#{labBillCollectingController.feeChanged}" />
                                                    </h:inputText>
                                                </p:column>

                                                <p:column>
                                                    <f:facet name="header">Payee</f:facet>
                                                    <h:outputLabel value="#{bif.institution.name}" rendered="#{bif.institution!=null}" ></h:outputLabel>
                                                    <h:outputLabel value="#{bif.department.name}" rendered="#{bif.department!=null}" ></h:outputLabel>
                                                    <h:outputLabel value="#{bif.staff.person.name}" rendered="#{bif.staff!=null}" ></h:outputLabel>
                                                </p:column>
                                            </p:dataTable>
                                        </p:tab>
                                    </p:tabView>
                                </h:panelGroup>

                                <h:panelGroup id="panelBillTotals" >

                                    <p:panel header="Bill Details" id="pBillDetails" style="padding: 5px; margin: 10px;" >
                                        <p:panelGrid columns="2" columnClasses="numberCol, textCol"  >
                                            <h:outputLabel value="Total" ></h:outputLabel>
                                            <h:outputLabel id="tot" value="#{labBillCollectingController.total}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </h:outputLabel>
                                            <h:outputLabel value="Discount" ></h:outputLabel>
                                            <h:outputLabel id="dis" value="#{labBillCollectingController.discount}" ></h:outputLabel>
                                            <h:outputLabel value="Net Total" ></h:outputLabel>
                                            <h:outputLabel id="netTotal" value="#{labBillCollectingController.netTotal}" ></h:outputLabel>
                                            <h:outputLabel value="VAT" ></h:outputLabel>
                                            <h:outputLabel id="vat" value="#{labBillCollectingController.vat}" ></h:outputLabel>
                                            <h:outputLabel value="VAT Plus Nettotal" ></h:outputLabel>
                                            <h:outputLabel id="vatTot" value="#{labBillCollectingController.vatPlusNetTotal}" ></h:outputLabel>
                                            
                                            <h:outputLabel value="Tendered" ></h:outputLabel>
                                            <h:inputText autocomplete="off" id="paid" value="#{labBillCollectingController.cashPaid}" >
                                                <p:ajax process="netTotal paid vat" update="balance" event="keyup"/>
                                            </h:inputText>
                                            <h:outputLabel  value="Balance " ></h:outputLabel>
                                            <h:outputLabel id="balance" value="#{labBillCollectingController.cashBalance}" ></h:outputLabel>
                                        </p:panelGrid>

                                    </p:panel>
                                </h:panelGroup>
                            </h:panelGrid>


                        </h:panelGrid>

                    </h:panelGroup>



                </p:panel>
            </h:panelGroup>

            <h:panelGroup rendered="#{labBillCollectingController.printPreview}" >
                
                
                <h:panelGroup rendered="#{sessionController.loggedPreference.printLabelForOPdBill eq true}">
                        
                        <div class="nonPrintBlock">
                            <p:commandButton value="Print" ajax="false" action="#"  >
                                <p:printer target="gpBillPreviewCoop" ></p:printer>
                            </p:commandButton>

                            <p:commandButton value="New Bill" ajax="false" action="#{billController.prepareNewBill()}" >
                            </p:commandButton>

                            <p:commandButton ajax="false" value="Batch Cancel" action="#{billController.cancellAll}" style="float: right;"
                                             onclick="if (!confirm('Are you sure you want to Cancel This Bill ?'))
                                                     return false;"/>
                        </div>
                        <h:panelGroup id="gpBillPreviewCoop">
                            <ui:repeat value="#{labBillCollectingController.bills}" var="b">
                            <bi:opdBillPrintCoop bill="#{b}" dup="false"/>                        
                        </ui:repeat>
                        </h:panelGroup>
                    </h:panelGroup>

                <p:panelGrid rendered="#{sessionContloggedPreferenceference.printLabelForOPdBill eq false}">
                <div class="nonPrintBlock" >
                    <p:commandButton value="Print" ajax="false" action="#">
                        <p:printer target="gpBillPreview" ></p:printer>
                    </p:commandButton>
                    <p:commandButton value="New Bill" ajax="false" action="/lab/bill_collecting_centre" actionListener="#{labBillCollectingController.prepareNewBill()}" onclick="onSubmitButton();"  >
                    </p:commandButton>

                </div>
                <div >


                    <h:panelGroup   id="gpBillPreview"   >






                        <div Class="fiveinchbillBreak" style="font-size: 150%; margin: 5px;">

                            <div >


                                <div class="receiptnoPrinting" >
                                    <h:outputLabel value="#{labBillCollectingController.current.insId}">
                                    </h:outputLabel>
                                </div>

                                <div class="billdatePrinting" >
                                    <h:outputLabel value="#{labBillCollectingController.current.billDate}">
                                        <f:convertDateTime pattern="dd/MM/yy"/>
                                    </h:outputLabel>
                                </div>

                                <div class="paymentPrinting" >
                                    <h:outputLabel value="#{labBillCollectingController.current.paymentMethod}">
                                        
                                    </h:outputLabel>
                                </div>

                                <div class="billtimePrinting" >
                                    <h:outputLabel value="#{labBillCollectingController.current.billTime}"
                                                   >
                                        <f:convertDateTime pattern="hh mm ss a"/>
                                    </h:outputLabel>
                                </div>

                                <div class="ptnamePrinting" >
                                    <h:outputLabel value="#{labBillCollectingController.current.patient.person.nameWithTitle}"
                                                   >
                                    </h:outputLabel>
                                </div>

                                <div class="phonePrinting" >
                                    <h:outputLabel value="#{labBillCollectingController.current.patient.person.phone}"
                                                   >
                                    </h:outputLabel>
                                </div>

                                <div class="cashierPrinting" >
                                    <h:outputLabel value="Cashier : #{labBillCollectingController.current.creater.code}">
                                    </h:outputLabel>
                                </div>

                                <div class="consultPrinting" >
                                    <h:outputLabel value="#{labBillCollectingController.current.collectingCentre.name}" />
                                    <h:outputLabel value="#{labBillCollectingController.current.creditCompany.name}" />
                                </div>



                                <div class="itemsPrinting" >
                                    <h:dataTable value="#{labBillCollectingController.lstBillEntries}" var="bip"   >
                                        <h:column >
                                            <h:outputLabel rendered="#{bip.billItem.item.department.id eq bill.toDepartment.id}" value="#{bip.billItem.item.printName}" style="margin: 4px; font-size: 11px!important;" >
                                            </h:outputLabel>
                                            <h:panelGroup rendered="#{bip.billItem.item.department.id eq bill.toDepartment.id and bip.billItem.billSession ne null }"  >
                                                <br/>
                                                <h:outputLabel value="Session No . " ></h:outputLabel>
                                                <h:outputLabel value="#{bip.billItem.billSession.serialNo}" style="margin: 4px; font-size: 11px!important;" >
                                                </h:outputLabel>
                                            </h:panelGroup>
                                        </h:column>
                                    </h:dataTable>
                                </div>

                                <div class="ratesPrinting"  >
                                    <h:dataTable value="#{labBillCollectingController.lstBillEntries}" var="bip1"  >
                                        <h:column >
                                            <h:outputLabel  rendered="#{bip1.billItem.bill.id eq bill.id}"  value="#{bip1.billItem.grossValue}"  style="margin: 4px; font-size: 11px!important;" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </h:outputLabel>
                                        </h:column>
                                    </h:dataTable>
                                </div>

                                <div class="totalsPrinting" >

                                    <table>
                                        <tr>
                                            <td>
                                                <br/>
                                                <br/>
                                            </td>
                                            <td>
                                                <br/>
                                                <br/>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; min-width: 3cm;">
                                                <h:outputLabel value="TOTAL" style="  text-align: right!important;  font-size: 15px!important; font-weight: bold; "/>
                                            </td>
                                            <td style="text-align: right!important; ">
                                                <h:outputLabel value="#{labBillCollectingController.current.total}"  style=" font-weight: bold ; text-align: right!important; font-size: 15px!important;    ">
                                                    <f:convertNumber pattern="#,##0.00" />
                                                </h:outputLabel>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left;">
                                                <h:outputLabel  rendered="#{labBillCollectingController.current.discount ne 0.0}" style=" text-align: right!important;  font-size: 15px!important;   "  value="Discount"/>
                                            </td>
                                            <td style="text-align: right!important; ">
                                                <h:outputLabel rendered="#{bill.discount ne 0.0}"   value="#{labBillCollectingController.current.discount}"  style=" text-align: right!important;  font-size: 15px!important;   ">
                                                    <f:convertNumber pattern="#,##0.00" />
                                                </h:outputLabel>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left;">
                                                <h:outputLabel  rendered="#{labBillCollectingController.current.discount ne 0.0}"    value="Net Total" style=" text-align: right!important;  font-size: 15px!important;   "/>
                                            </td>
                                            <td style="text-align: right!important; ">
                                                <h:outputLabel  rendered="#{labBillCollectingController.current.discount ne 0.0}"    value="#{bill.netTotal}"  style="font-weight: bold ; text-align: right!important; font-size: 15px!important;    ">
                                                    <f:convertNumber pattern="#,##0.00" />
                                                </h:outputLabel>
                                            </td>
                                        </tr>


                                    </table>

                                </div>

                            </div>

                        </div>





                    </h:panelGroup>




                </div>
                </p:panelGrid>



            </h:panelGroup>


        </h:form>

    </ui:define>
</ui:composition>
